package com.example.tests;

import com.example.utils.SortedListOf;
import org.testng.annotations.Test;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class AddressModificationTestSet extends TestBase {

    @Test(dataProvider = "randomValidAddressDataGenerator")
    public void ModifySomeAddressEntryListComparsion(AddressData addressData) throws Exception{

        //save old state from DB
        SortedListOf<AddressData> oldAddressList
                =new SortedListOf<AddressData>(applicationManager.getModel().getAddressList());

        //get addresses number and check if there are any addresses
        int addressCount = oldAddressList.size();
        if (addressCount == 0)
            return;

        //pick an object to modify
        int randomAddressNumber = applicationManager.getAddressHelper().getRandomIndex(addressCount);

        //modify address and no submit
        applicationManager.getAddressHelper()
                .initAddressModification(randomAddressNumber);

        //read data from card
        AddressData displayedAddressData = applicationManager.getAddressHelper()
                .readAddressDataFromCard();

        //find needed address in the DB
        AddressData storedAddressData = oldAddressList.get(randomAddressNumber);

        //assure that displayed address card is equal to stored address data
        assertTrue(displayedAddressData.completelyEquals(storedAddressData));

        //modify data and submit
        AddressData expectedAddressData = applicationManager.getAddressHelper()
                .fillAddressModificationFormAndSubmit(addressData, randomAddressNumber);

        //save new state from gui
        SortedListOf<AddressData> newAddressList
                = applicationManager.getAddressHelper()
                .getGuiAddressList();

        //compare states
        assertThat(newAddressList, equalTo(oldAddressList.without(randomAddressNumber).withAdded(expectedAddressData)));
    }
}
